CodeMirror.defineMode("razor", function (config, parserConfig) {

    var keywords = keywords("abstract as base bool break byte case catch char checked class const continue DateTime decimal default delegate do double dynamic else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using var virtual void volatile while");

    var indentUnit = config.indentUnit;
    var Kludges = parserConfig.htmlMode ? {
        autoSelfClosers: { "br": true, "img": true, "hr": true, "link": true, "input": true,
            "meta": true, "col": true, "frame": true, "base": true, "area": true
        },
        doNotIndent: { "pre": true, "!cdata": true },
        allowUnquoted: true
    } : { autoSelfClosers: {}, doNotIndent: { "!cdata": true }, allowUnquoted: false };
    var alignCDATA = parserConfig.alignCDATA;

    // Return variables for tokenizers
    var tagName, type;

    function inText(stream, state) {
        function chain(parser) {
            state.tokenize = parser;
            return parser(stream, state);
        }

        var ch = stream.next();
        if (ch == "<") {
            if (stream.eat("!")) {
                if (stream.eat("[")) {
                    if (stream.match("[CDATA[")) return chain(inBlock("xml-cdata", "]]>"));
                    else return null;
                }
                else if (stream.match("--")) return chain(inBlock("xml-comment", "-->"));
                else if (stream.match("DOCTYPE")) {
                    stream.eatWhile(/[\w\._\-]/);
                    return chain(inBlock("xml-doctype", ">"));
                }
                else return null;
            }
            else if (stream.eat("?")) {
                stream.eatWhile(/[\w\._\-]/);
                state.tokenize = inBlock("xml-processing", "?>");
                return "xml-processing";
            }
            else {
                type = stream.eat("/") ? "closeTag" : "openTag";
                stream.eatSpace();
                tagName = "";
                var c;
                while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
                state.tokenize = inTag;
                return "xml-tag";
            }
        }
        else if (ch == "&") {
            stream.eatWhile(/[^;]/);
            stream.eat(";");
            return "xml-entity";
        }
        // text mode
        else if (ch == "@") {
            if (stream.peek() != "@") { // handle @@ escaping
                state.tokenize = inRazor(inText);
                return "razor-tag";
            }

            stream.next()
        }
        else {
            stream.eatWhile(/[^&<@]/);
            return null;
        }
    }

    function inTag(stream, state) {
        var ch = stream.next();
        if (ch == ">" || (ch == "/" && stream.eat(">"))) {
            state.tokenize = inText;
            type = ch == ">" ? "endTag" : "selfcloseTag";
            return "xml-tag";
        }
        else if (ch == "=") {
            type = "equals";
            return null;
        }
        else if (/[\'\"]/.test(ch)) {
            state.tokenize = inAttribute(ch);
            return state.tokenize(stream, state);
        }
        else {
            stream.eatWhile(/[^\s\u00a0=<>\"\'\/?]/);
            return "xml-word";
        }
    }

    function inAttribute(quote) {
        return function (stream, state) {
            var razor = false;
            while (!stream.eol()) {
                if (stream.peek() == "@") {
                    state.tokenize = inRazorAttribute(quote);
                    break;
                }
                else if (stream.next() == quote) {
                    state.tokenize = inTag;
                    break;
                }
            }
            return "xml-attribute";
        };
    }

    function inRazorAttribute(quote) {
        return function (stream, state) {
            stream.eat("@");
            state.tokenize = inRazor(inAttribute(quote), "xml-attribute");
            return "razor-tag";
        }
    }

    function inBlock(style, terminator) {
        return function (stream, state) {
            while (!stream.eol()) {
                if (stream.match(terminator)) {
                    state.tokenize = inText;
                    break;
                }
                stream.next();
            }
            return style;
        };
    }

    function inRazor(nextState, style) {
        return function (stream, state) {
            if (stream.match("using")) {
                state.tokenize = inRazorUsing(nextState, style);
                return "razor-keyword";
            }
            else if (stream.match("if")) {
                state.tokenize = inRazorIf(nextState, style);
                return "razor-keyword";
            }
            else if (stream.match("{")) {
                state.tokenize = inRazorBlock(1, nextState);
            }
            else {
                stream.eatWhile(/[^\s\'\"<@]/);
                state.tokenize = nextState;
            }

            return style + " razor";
        }
    }

    function inRazorUsing(nextState, style) {
        return function (stream, state) {
            stream.eatWhile(/[^;]/);
            stream.eat(";");
            state.tokenize = nextState;
            return style + " razor";
        }
    }

    function inRazorIf(nextState, style) {
        return function (stream, state) {
            while (!stream.eol()) stream.next();
            state.tokenize = nextState;
            return style + " razor";
        }
    }

    function inRazorBlock(nestedLevel, nextState) {
        var nested = nestedLevel || 1;

        return function (stream, state) {
            while (!stream.eol()) {

                // identify keywords
                if (stream.eatWhile(/\w/)) {
                    state.tokenize = inRazorBlock(nested, nextState);

                    if (keywords[stream.current()]) {
                        stream.eatSpace();
                        return "razor-keyword";
                    }

                    stream.next();
                    break;
                }

                if (stream.peek() == '"') {
                    state.tokenize = inRazorString(inRazorBlock(nested, nextState));
                    break;
                }

                if (stream.peek() == '{') nested++;
                if (stream.peek() == '}') nested--;

                if (nested == 0) {
                    state.tokenize = inRazorBlockEnd(nextState);
                    break;
                }

                if (stream.eatWhile(/[^\w{}\"]/)) {
                    stream.eatSpace();
                    state.tokenize = inRazorBlock(nested, nextState);
                    break;
                }

                stream.next();
            }

            return "razor";
        };
    }

    function inRazorBlockEnd(nextState) {
        return function (stream, state) {
            stream.eat("}");
            state.tokenize = nextState;
            return "razor-tag";
        }
    }

    function inRazorString(nextState) {
        return function (stream, state) {
            // when in this state, the " is not read yet
            stream.eat('"');
            stream.eatWhile(/[^"]/);
            stream.eat('"');
            state.tokenize = nextState;
            return "razor-string";
        }
    }

    var curState, setStyle;

    function pass() {
        for (var i = arguments.length - 1; i >= 0; i--) curState.cc.push(arguments[i]);
    }

    function cont() {
        pass.apply(null, arguments);
        return true;
    }

    function pushContext(tagName, startOfLine) {
        var noIndent = Kludges.doNotIndent.hasOwnProperty(tagName) || (curState.context && curState.context.noIndent);
        curState.context = {
            prev: curState.context,
            tagName: tagName,
            indent: curState.indented,
            startOfLine: startOfLine,
            noIndent: noIndent
        };
    }
    function popContext() {
        if (curState.context) curState.context = curState.context.prev;
    }

    function element(type) {
        if (type == "openTag") { curState.tagName = tagName; return cont(attributes, endtag(curState.startOfLine)); }
        else if (type == "closeTag") { popContext(); return cont(endclosetag); }
        else if (type == "xml-cdata") {
            if (!curState.context || curState.context.name != "!cdata") pushContext("!cdata");
            if (curState.tokenize == inText) popContext();
            return cont();
        }
        else return cont();
    }
    function endtag(startOfLine) {
        return function (type) {
            if (type == "selfcloseTag" ||
          (type == "endTag" && Kludges.autoSelfClosers.hasOwnProperty(curState.tagName.toLowerCase())))
                return cont();
            if (type == "endTag") { pushContext(curState.tagName, startOfLine); return cont(); }
            return cont();
        };
    }
    function endclosetag(type) {
        if (type == "endTag") return cont();
        return pass();
    }

    function attributes(type) {
        if (type == "xml-word") { setStyle = "xml-attname"; return cont(attributes); }
        if (type == "equals") return cont(attvalue, attributes);
        return pass();
    }
    function attvalue(type) {
        if (type == "xml-word" && Kludges.allowUnquoted) { setStyle = "xml-attribute"; return cont(); }
        if (type == "xml-attribute") return cont();
        return pass();
    }

    function keywords(str) {
        var obj = {}, words = str.split(" ");
        for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
        return obj;
    }

    return {
        startState: function () {
            return { tokenize: inText, cc: [], indented: 0, startOfLine: true, tagName: null, context: null };
        },

        token: function (stream, state) {
            if (stream.sol()) {
                state.startOfLine = true;
                state.indented = stream.indentation();
            }
            if (stream.eatSpace()) return null;

            setStyle = type = tagName = null;
            var style = state.tokenize(stream, state);
            if ((style || type) && style != "xml-comment") {
                curState = state;
                while (true) {
                    var comb = state.cc.pop() || element;
                    if (comb(type || style)) break;
                }
            }
            state.startOfLine = false;
            return setStyle || style;
        },

        indent: function (state, textAfter) {
            var context = state.context;
            if (context && context.noIndent) return 0;
            if (alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0;
            if (context && /^<\//.test(textAfter))
                context = context.prev;
            while (context && !context.startOfLine)
                context = context.prev;
            if (context) return context.indent + indentUnit;
            else return 0;
        },

        electricChars: "/"
    };
});

CodeMirror.defineMIME("application/xml", "xml");
CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true});
